//
//  AppDelegate+MMGBackgroundTask.m
//  DollsVending
//
//  Created by Don on 2017/11/5.
//  Copyright © 2017年 Memego, Inc. All rights reserved.
//

#import "AppDelegate+MMGBackgroundTask.h"
#import <objc/runtime.h>

@interface AppDelegate ()
@property (nonatomic, assign) UIBackgroundTaskIdentifier bgTask;
@end
@implementation AppDelegate (MMGBackgroundTask)
- (void)mmg_backgroundTask {
    [self comeToBackgroundMode];
}

- (void)comeToBackgroundMode{
    //初始化一个后台任务BackgroundTask，这个后台任务的作用就是告诉系统当前app在后台有任务处理，需要时间
    UIApplication*  app = [UIApplication sharedApplication];
    self.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
    }];
    //开启定时器 不断向系统请求后台任务执行的时间
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:25.0 target:self selector:@selector(applyForMoreTime) userInfo:nil repeats:YES];
    [timer fire];
}

- (void)applyForMoreTime {
    //如果系统给的剩余时间小于60秒 就终止当前的后台任务，再重新初始化一个后台任务，重新让系统分配时间，这样一直循环下去，保持APP在后台一直处于active状态。
    if ([UIApplication sharedApplication].backgroundTimeRemaining < 60) {
        [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
        self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
            self.bgTask = UIBackgroundTaskInvalid;
        }];
    }
}


- (UIBackgroundTaskIdentifier)bgTask {
    return [objc_getAssociatedObject(self, @selector(bgTask)) unsignedIntegerValue];
}
- (void)setBgTask:(UIBackgroundTaskIdentifier)bgTask {
    objc_setAssociatedObject(self, @selector(bgTask), @(bgTask), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end
